﻿using System;
using System.Windows.Input;

namespace HeyoUi.Controllers
{
    public class DelegateCommand : ICommand
    {
        public Func<object, bool> CanExecuteFunc;
        public Action<object> ExecuteAction;
        public bool CanExecuteCache;

        public DelegateCommand()
        {

        }
        public DelegateCommand(Action<object> executeAction)
        {
            ExecuteAction = executeAction;
        }
        public bool CanExecute(object parameter)
        {
            if (CanExecuteCache && CanExecuteFunc != null)
            {
                return CanExecuteFunc.Invoke(parameter);
            }
            //if (null == parameter) return true;
            //Type type = parameter.GetType();
            //if ("System.Windows.Controls.Slider" != type.ToString()) return true;
            //System.Windows.Controls.Slider slider = (System.Windows.Controls.Slider)parameter;

            //return slider.IsMouseCaptureWithin || slider.IsKeyboardFocusWithin;
            return true;
        }
        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            ExecuteAction(parameter);
        }

        protected virtual void OnCanExecuteChanged()
        {
            CanExecuteChanged?.Invoke(this, EventArgs.Empty);
        }
    }
}
